﻿-- =============================================
-- Author:      Sergey Solyanik
-- Create date: 7/25/09
-- Description: Adds code reviewer
-- =============================================
CREATE PROCEDURE [dbo].[AddReviewer]
	@ReviewerAlias NVARCHAR (50),
	@DocumentSetId INT,
	@Result INT OUTPUT
AS
    DECLARE @ReviewerId int
    SET @ReviewerId = (SELECT Id FROM dbo.Reviewer
        WHERE DocumentSetId = @DocumentSetId AND UserAlias = @ReviewerAlias)
    IF @ReviewerId IS NOT NULL
    BEGIN
        SET @result = @ReviewerId
        RETURN
    END
    INSERT INTO dbo.Reviewer (UserAlias, DocumentSetId)
        VALUES(@ReviewerAlias, @DocumentSetId)
    SET @Result = @@IDENTITY
    INSERT INTO dbo.EmailRequest (ReviewerId) VALUES(@result)
RETURN 0